<template>
  <view class="login-container">
    <view class="login-form">
      <text class="title">图书数据采集系统</text>
      <input 
        v-model="username" 
        placeholder="请输入账号" 
        class="input"
      />
      <input 
        v-model="password" 
        placeholder="请输入密码" 
        password 
        class="input"
      />
      <button @click="handleLogin" class="login-btn">登录</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleLogin() {
      // 简单的验证
      if (!this.username.trim() || !this.password.trim()) {
        uni.showToast({ 
          title: '请输入账号和密码', 
          icon: 'none' 
        })
        return
      }
      
      // 模拟登录验证
      if (this.username === 'admin' && this.password === '123123') {
        // 保存token到本地存储
        uni.setStorageSync('token', 'admin_token')
        uni.setStorageSync('userInfo', {
          username: this.username,
          loginTime: new Date()
        })
        
        uni.showToast({ 
          title: '登录成功', 
          icon: 'success',
          duration: 1500
        })
        
        // 延迟跳转，让Toast显示完整
        setTimeout(() => {
          // 跳转到首页 - 根据首页是否是tabBar页面选择不同的API
          uni.switchTab({ 
            url: '/pages/index/index' 
          })
        }, 1500)
      } else {
        uni.showToast({ 
          title: '账号或密码错误', 
          icon: 'none' 
        })
      }
    }
  },
  onLoad() {
    // 页面加载时检查是否已登录
    const token = uni.getStorageSync('token')
    if (token) {
      // 如果已登录，直接跳转到首页
      uni.switchTab({ 
        url: '/pages/index/index' 
      })
    }
  }
}
</script>

<style>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-form {
  background: white;
  padding: 40rpx;
  border-radius: 20rpx;
  width: 80%;
  box-shadow: 0 10rpx 30rpx rgba(0,0,0,0.1);
}

.title {
  display: block;
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 60rpx;
  color: #333;
}

.input {
  border: 1rpx solid #ddd;
  padding: 20rpx;
  margin-bottom: 30rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
}

.login-btn {
  background: #007AFF;
  color: white;
  border: none;
  padding: 20rpx;
  border-radius: 10rpx;
  font-size: 32rpx;
  margin-top: 20rpx;
}

.login-btn:active {
  background: #0056CC;
}
</style>